//
//  RMMRootViewController.m
//  Romomo
//
//  Created by puman on 16/9/30.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMRootViewController.h"
#import "RMMTabBarViewController.h"

@interface RMMRootViewController ()<UIScrollViewDelegate>

@end

@implementation RMMRootViewController
{
    UIPageControl *_pageControl;
    RMMNotNetWorkView *_networkView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageView.image = [UIImage imageNamed:@"开机-7"];
    [self.view addSubview:imageView];
    UIView *backView = [[UIView alloc] initWithFrame:imageView.bounds];
    backView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    [imageView addSubview:backView];
//    UIImage * sourceImage = [UIImage imageNamed:@"开机-7"];
//    CIImage * ciImage    = [[CIImage alloc] initWithImage:sourceImage];
//    CIFilter * blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
//    //将图片输入到滤镜中
//    [blurFilter setValue:ciImage forKey:kCIInputImageKey];
//    //设置模糊程度
//    [blurFilter setValue:@(5) forKey:@"inputRadius"];
//    //将处理之后的图片输出
//    CIImage * outCiImage    = [blurFilter valueForKey:kCIOutputImageKey];
//    CIContext * context      = [CIContext contextWithOptions:nil];
//    //获取CGImage句柄
//    CGImageRef outCGImageRef = [context createCGImage:outCiImage fromRect:[outCiImage extent]];
//    UIImage * resultImage    = [UIImage imageWithCGImage:outCGImageRef];
//    
//    //释放句柄
//    CGImageRelease(outCGImageRef);
//    
//    [imageView setImage:resultImage];
//    
//    [self.view addSubview:imageView];
    


    NSString *isFristComeIn = [[RMMFileManager shareHandle] getValeForKey:@"ISFRISTCOMEIN"];
    if (![isFristComeIn isEqualToString:@"1"]) {
        [[RMMFileManager shareHandle] saveVale:@"1" forKey:@"ISFRISTCOMEIN"];
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        scrollView.contentSize = CGSizeMake(kScreenWideth * 3, kScreenHeight);
        scrollView.pagingEnabled = YES;
        scrollView.bounces = NO;
        scrollView.delegate = self;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:scrollView];
        
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(kScreenWideth / 2 - kRow, 55, 20, kRow)];
        _pageControl.currentPage = 0;
        _pageControl.numberOfPages = 3;
        _pageControl.pageIndicatorTintColor = COLORFROMCODE(0xD7D7D7, 1);
        _pageControl.currentPageIndicatorTintColor = COLORFROMCODE(0x53C7C6, 1);
        [self.view addSubview:_pageControl];
        
        NSArray *imageArray = @[@"引导-1@2x",@"引导-2@2x",@"引导-3@2x"];
        if (kScreenWideth == 320) {
            imageArray = @[@"iPhone SE-1",@"iPhone SE-2",@"iPhone SE-3"];
            
        } else if (kScreenWideth == 375) {
            imageArray = @[@"引导-1@2x",@"引导-2@2x",@"引导-323@2x"];
        } else if (kScreenWideth == 414){
            imageArray = @[@"iPhone 7 Plus-1",@"iPhone 7 Plus-2",@"iPhone 7 Plus-3"];
        }
        for (int i = 0; i < 3; i++) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(kScreenWideth * i, 0, kScreenWideth, kScreenHeight)];
            view.backgroundColor = kWhiteColor;
            [scrollView addSubview:view];
            
            UIImage *image = [UIImage imageNamed:imageArray[i]];
            UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight)];
            imageV.image = image;
            imageV.backgroundColor = kWhiteColor;
            [view addSubview:imageV];
            if (i == 2) {
                imageV.userInteractionEnabled = YES;
                UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
                button.frame = CGRectMake(kScreenWideth / 2 - 80, imageV.height - 76, 3 * kHalf + kRow, 44);
                button.layer.cornerRadius = 22;
                button.layer.borderWidth = 1;
                button.layer.borderColor = COLORFROMCODE(0x53C7C6 , 1).CGColor;
                [button setTitleColor:COLORFROMCODE(0x53C7C6 , 1) forState:(UIControlStateNormal)];
                button.titleLabel.font = kBFont(13);
                [button setTitle:@"立即体验" forState:(UIControlStateNormal)];
                BUTTON_S(button, chlick:);
                button.tag = 1 + i;
                [imageV addSubview:button];
            }
        }
    } else {
        _networkView = [[RMMNotNetWorkView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight)];
        _networkView.hidden = YES;
        BUTTON_S(_networkView.button, networkBut);
        [self.view addSubview:_networkView];

        NSString *cust_id = [[RMMFileManager shareHandle] getValeForKey:@"CUST_ID"];
        if (cust_id.length > 0) {
            [RMMFileManager shareHandle].isLoginSuccess = YES;
            [[RMMFileManager shareHandle].userDic setValue:cust_id forKey:@"CUST_ID"];
            [self getDataWith:cust_id];
        } else {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [RMMFileManager shareHandle].fuWuType = @"1";
                UIWindow *window = [UIApplication sharedApplication].keyWindow;
                window.rootViewController = [[RMMTabBarViewController alloc] init];
            });
        }
    }
}
- (void)checkVersionUpDate{
    
}
- (void)addAlertController{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"版本提示" message:@"您有新的版本了可以更新了" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *actionA = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *actionB = [UIAlertAction actionWithTitle:@"前往更新" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];;

}
- (void)networkBut{
    NSString *cust_id = [[RMMFileManager shareHandle] getValeForKey:@"CUST_ID"];
    [self getDataWith:cust_id];
}
- (void)getDataWith:(NSString *)string{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"cust_id"] = string;
    NSString *url = UserInfo;
    [OSRequestManager requestWithUrlString:url parDic:parDic network:^(BOOL isNetwork) {
        if (!isNetwork) {
            _networkView.hidden = NO;
        } else {
            _networkView.hidden = YES;
        }
    } method:GET finish:^(NSDictionary *dataDic) {
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        _networkView.hidden = YES;
        if ([status isEqualToString:@"1"]) {
            [RMMFileManager shareHandle].fuWuType = @"1";
            if ([dataDic[@"data"] isKindOfClass:[NSDictionary class]]) {
                [RMMFileManager shareHandle].userDic = [RMMFileManager setValueForDic:dataDic[@"data"]];
            }
           // [[RMMFileManager shareHandle] saveVale:[NSString stringWithFormat:@"%@",dataDic[@"data"][@"CUST_ID"]] forKey:@"CUST_ID"];

            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            window.rootViewController = [[RMMTabBarViewController alloc] init];
        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
            _networkView.hidden = NO;
        }

    } error:^(NSError *errors) {
        _networkView.hidden = NO;
    } view:self.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat x = scrollView.contentOffset.x;
    if (x == 0) {
        _pageControl.currentPage = 0;
    } else if (x == kScreenWideth) {
        _pageControl.currentPage = 1;
    } else {
        _pageControl.currentPage = 2;
    }
}
- (void)chlick:(UIButton *)but{
    [[RMMFileManager shareHandle] saveVale:@"1" forKey:@"ISFRISTCOMEIN"];
    [RMMFileManager shareHandle].fuWuType = @"1";
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[RMMTabBarViewController alloc] init];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
